<template>
  <a-card class="card" :bordered="false">
    <a-form :form="form">
      <a-row :gutter="24">
        <a-col :md="12" :sm="24">
          <a-form-item
            label="SN/LOT"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback
          >
            <a-input ref="barcodeInput" allow-clear placeholder="请输入SN/LOT" v-decorator="['barcode', {rules: [{required: true, message: '请输入SN/LOT！'}]}]" @pressEnter="pressEnterBarCode"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-card>
</template>
<script>
  import { selectByBarcode } from '@/api/modular/main/stock/stockManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        currentData: { qty: 1 },
        barcodes: [],
        form: this.$form.createForm(this)
      }
    },
    created () {
    },
    activated () {
      this.handleCancel()
      setTimeout(() => {
        this.$refs.barcodeInput.focus()
      }, 100)
    },
    mounted () {
    },
    methods: {
      removeScanedBarcode (code) {
        const newData = [...this.barcodes]
        newData.splice(this.barcodes.indexOf(code), 1)
        this.barcodes = newData
      },
      clearPreData () {
        this.currentData.barcode = ''
        this.currentData.materialCode = ''
        this.currentData.materialName = ''
        this.currentData.materialModel = ''
        this.currentData.unitPrice = ''
        this.currentData.unit = ''
        this.currentData.qty = ''
      },
      handleSubmit () {
        let result = {}
        this.$emit('sure', this.currentData, val => {
          result = val
        })
        if (result) {
          this.$message.success('保存成功，请继续操作')
          this.barcodes.push(this.currentData.barcode)
        }
        this.$refs.barcodeInput.focus()
        this.resetBarcode()
      },
      handleCancel () {
        this.form.resetFields()
        this.currentData = {}
      },
      pressEnterBarCode () {
        const barcode = this.form.getFieldValue('barcode')
        if (!barcode || barcode.length === 0) {
          this.$message.error('SN/LOT不能为空')
          return
        }
        if (this.barcodes.find(item => item === barcode)) {
          this.$message.error('该SN/LOT已扫描:' + barcode)
          this.resetBarcode()
        } else {
          selectByBarcode(barcode).then((res) => {
            if (res.success) {
              if (res.data) {
                this.currentData.barcode = barcode
                this.currentData.materialCode = res.data.materialCode
                this.currentData.materialName = res.data.materialName
                this.currentData.materialModel = res.data.materialModel
                this.currentData.unit = res.data.unit
                this.currentData.barcodeType = res.data.barcodeType
                this.currentData.unitPrice = res.data.unitPrice
                this.currentData.qty = res.data.qty
                this.handleSubmit()
              } else {
                this.$message.error('该条码号不在当前库存中:' + barcode)
                this.resetBarcode()
                this.$refs.barcodeInput.focus()
              }
            } else {
              this.$message.error('条码信息异常:' + res.message)
              this.resetBarcode()
              this.$refs.barcodeInput.focus()
            }
          })
        }
      },
      resetBarcode () {
        this.form.setFieldsValue(
          {
            'barcode': ''
          }
        )
      },
      clearBarcodes () {
        this.barcodes = []
      }
    }
  }
</script>
<style lang="less">
  .ant-card-body {
      padding: 10px;
  }
</style>
